//
//  SYHomeTBVC.swift
//  SYWeibo
//
//  Created by Saya on 16/4/6.
//  Copyright © 2016年 黄金果. All rights reserved.


import UIKit
import SVProgressHUD

private let indentfy:String = "HomeCell"
private let refreshControlHeight:CGFloat = 60

class SYHomeTBVC: SYBaseTBVC {
    
    private lazy var statusViewModel : SYTVStatusViewModel = SYTVStatusViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpUI()

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
//        tableView.reloadData()
    }
    
    private func setUpUI (){
        if !isLogin{
            visitView?.setUpVisitView("关注一些人,回到这里看看有什么惊喜", bigImgName: "visitordiscover_feed_image_smallicon", isHome: true)
            return
        }
        loadData()
        perpareForTable()

        navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_pop", target: self, action: #selector(SYHomeTBVC.pushBtnDidClick))
        
        self.tableView.registerClass(SYHomeCell.self, forCellReuseIdentifier: indentfy)
        tableView.tableFooterView = indicatorView
//        refreshControl = UIRefreshControl()
//        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        //添加自定义下啦刷新控件
    
    }
    
    private func perpareForTable (){
        //为了测试占时将tablecell的高度设置为250
        //        tableView.rowHeight = 250
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        //去掉分割线
        tableView.separatorStyle = .None
        
        
        tableView.addSubview(freshView)
        freshView.addTarget(self, action: #selector(SYHomeTBVC.loadData), forControlEvents: .ValueChanged)
        
    }

    
    func pushBtnDidClick () {
//        print("popBtnDidClick")
        let temVC = SYTempVC()
        navigationController?.pushViewController(temVC, animated: true)
    }
    
    @objc private func loadData() {
        //colse refresh
//        refreshControl?.endRefreshing()

        self.statusViewModel.loadHomeData(indicatorView.isAnimating()) { (isDownLoaded) -> () in
            self.freshView.endRefresh()
            if !isDownLoaded {
                SVProgressHUD.showErrorWithStatus(errorMessage)
                return
            }
            print("获取home数据成功")
            self.indicatorView.stopAnimating()
            self.tableView.reloadData()
        }
    }
    
    //懒加载控件
    private lazy var indicatorView:UIActivityIndicatorView = UIActivityIndicatorView()
    private lazy var freshView :SYRefreshControl = SYRefreshControl()

}

extension SYHomeTBVC{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.cellStatusViewModel.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let currentCellModel = statusViewModel.cellStatusViewModel[indexPath.row]
        //使用这个方法无需手动创建cell,但是需要注册cell,否则会奔溃
        let cell = tableView.dequeueReusableCellWithIdentifier(indentfy, forIndexPath: indexPath) as! SYHomeCell
        cell.statusModel = currentCellModel
        
        if !indicatorView.isAnimating() && statusViewModel.cellStatusViewModel.count - 1 == indexPath.row{
            indicatorView.startAnimating()
            loadData()
        }
        
        return cell
    }
    
}
